package com.mall.common.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.mall.b2c.util.SettingUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.mall.common.CommonSetting;
import com.mall.common.service.MailService;
import com.mall.common.service.TemplateService;
import com.mall.common.util.mail.MailSenderInfo;
import com.mall.common.util.mail.SimpleMailSender;

import freemarker.template.Configuration;
import freemarker.template.Template;


/**
 */
@Service
public class MailServiceImpl implements MailService{
	
	@Resource private FreeMarkerConfigurer freeMarkerConfigurer;
	@Resource private TemplateService templateService;

	/**
	 * 将数据放入模板，返回内容
	 * @param templateFilePath
	 * @param dataMap
	 * @return
	 */
    @SuppressWarnings("rawtypes")
	private String getMailText(String templateFilePath, Map dataMap){      
		String htmlText = "";
		try {
			// 通过指定模板名获取FreeMarker模板实例
			Configuration configuration = freeMarkerConfigurer.getConfiguration();
			//freemarkerManager.
			Template template = configuration.getTemplate(templateFilePath, "utf-8");
			// 解析模板并替换动态数据，最终数据将替换模板文件中的${}标签。
			htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(
					template, dataMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return htmlText;     
    } 
	
	/**
	 * 发送测试邮件
	 * @param smtpFromMail 发件人邮箱
	 * @param smtpHost SMTP服务器地址
	 * @param smtpPort SMTP服务器端口
	 * @param smtpUsername SMTP用户名
	 * @param smtpPassword SMTP密码
	 * @param toMail 收邮件人地址
	 */
	public Boolean sendTestMail(String smtpFromMail, String smtpHost, 
			String smtpPort, String smtpUsername, String smtpPassword, String toMail){
		//TODO 发送邮件
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shopName", SettingUtils.get().getName());
		String templatePath = templateService.getTemplatePathByName("smtpTest");
		String subject = "邮件设置测试";
		// 封装数据
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setFromAddress(smtpFromMail);
		mailInfo.setMailServerHost(smtpHost);
		mailInfo.setMailServerPort(smtpPort);
		mailInfo.setUserName(smtpUsername);
		mailInfo.setPassword(smtpPassword);
		mailInfo.setToAddress(toMail);
		mailInfo.setSubject(subject);
		mailInfo.setValidate(true);
		mailInfo.setContent(getMailText(templatePath, map));
		// 发送html格式
		return SimpleMailSender.sendHtmlMail(mailInfo);
	
	}

	@Override
	public Boolean sendMail(String templateName, String email, String title,
			Map<String, Object> params, CommonSetting setting) {
		String templatePath = templateService.getTemplatePathByName(templateName);
		if(StringUtils.isBlank(templatePath)){
			return false;
		}
		MailSenderInfo mailInfo = new MailSenderInfo(setting);
		mailInfo.setToAddress(email);
		mailInfo.setSubject(title);
		mailInfo.setContent(getMailText(templatePath, params));
		// 发送html格式
		return SimpleMailSender.sendHtmlMail(mailInfo);
	}
	
}
